package com.jnu.book.data;

import android.content.Context;


import com.jnu.book.R;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class DataBank {
    private static final String DATA_FILE_NAME="data";
    ArrayList<book> bookList;
    private final Context context;
    public DataBank(Context context) {
        this.context=context;
    }

    public ArrayList<book> loadData(){
        try{
            ObjectInputStream objectInputStream=new ObjectInputStream(context.openFileInput(DATA_FILE_NAME));
            bookList=(ArrayList<book>)objectInputStream.readObject();
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }

        /*bookList=new ArrayList<>();
        bookList.add(new book("软件项目管理案例教程（第4版）", R.drawable.book_2));
        bookList.add(new book("创新工程实践", R.drawable.book_no_name));
        bookList.add(new book("信息安全数学基础（第2版）", R.drawable.book_1));*/
        return bookList;
    }
    public void saveData(){
        ObjectOutputStream objectOutputStream=null;
        try{
            objectOutputStream = new ObjectOutputStream(context.openFileOutput(DATA_FILE_NAME,Context.MODE_PRIVATE));
            objectOutputStream.writeObject(bookList);
        }catch(
                IOException e){
            e.printStackTrace();
        }finally{
            try{
                if(objectOutputStream!=null){
                    objectOutputStream.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

}
